
using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Web.UI.Design;
using System.Xml;
using Microsoft.Web.Design;
using Microsoft.Web.Design.Interop;
using Microsoft.Web.Design.Remote;

namespace MyDesigner
{
	public class ProxyHierarchicalDataSourceControlDesigner : HierarchicalDataSourceDesigner
	{
		// Fields
		private ProxyControlDesignerImpl _impl;

		// Methods
		internal string CreateErrorDesignTimeHtmlInternal(string errorMessage)
		{
			return base.CreateErrorDesignTimeHtml(errorMessage);
		}

		internal string CreatePlaceHolderDesignTimeHtmlInternal(string message)
		{
			return base.CreatePlaceHolderDesignTimeHtml(message);
		}

		protected override void Dispose(bool disposing)
		{
			this._impl.Dispose(disposing);
			base.Dispose(disposing);
		}

		public override string GetDesignTimeHtml(DesignerRegionCollection regions)
		{
			return this._impl.GetDesignTimeHtml(regions);
		}

		public override string GetEditableDesignerRegionContent(EditableDesignerRegion region)
		{
			return this._impl.GetEditableDesignerRegionContent(region);
		}

		public override string GetPersistenceContent()
		{
			return this._impl.GetPersistenceContent();
		}

		internal IControlDesignerTag GetTag()
		{
			return base.Tag;
		}

		public override DesignerHierarchicalDataSourceView GetView(string viewName)
		{
			ProxyHierarchicalDataSourceDesignerImpl impl = this._impl as ProxyHierarchicalDataSourceDesignerImpl;
			if (impl == null)
			{
				return null;
			}
			return impl.GetView(viewName);
		}

		public override void Initialize(IComponent component)
		{
			base.Initialize(component);
			this._impl = new ProxyHierarchicalDataSourceDesignerImpl(this, base.Tag);
			this._impl.Initialize(component);
		}

		public override void OnComponentChanged(object sender, ComponentChangedEventArgs ce)
		{
			this._impl.OnComponentChanged(sender, ce);
		}

		public override void RefreshSchema(bool preferSilent)
		{
			ProxyHierarchicalDataSourceDesignerImpl impl = this._impl as ProxyHierarchicalDataSourceDesignerImpl;
			if (impl != null)
			{
				impl.RefreshSchema(preferSilent);
			}
		}

		public override void SetEditableDesignerRegionContent(EditableDesignerRegion region, string content)
		{
			this._impl.SetEditableDesignerRegionContent(region, content);
		}

		internal void SetViewFlagsInternal(ViewFlags viewFlags, bool setFlag)
		{
			base.SetViewFlags(viewFlags, setFlag);
		}

		// Properties
		public override DesignerActionListCollection ActionLists
		{
			get
			{
				DesignerActionListCollection actionLists = base.ActionLists;
				this._impl.FilterActionLists(ref actionLists);
				return actionLists;
			}
		}

		public override bool CanConfigure
		{
			get
			{
				ProxyHierarchicalDataSourceDesignerImpl impl = this._impl as ProxyHierarchicalDataSourceDesignerImpl;
				if (impl == null)
				{
					return false;
				}
				return impl.CanConfigure;
			}
		}

		public override bool CanRefreshSchema
		{
			get
			{
				ProxyHierarchicalDataSourceDesignerImpl impl = this._impl as ProxyHierarchicalDataSourceDesignerImpl;
				if (impl == null)
				{
					return false;
				}
				return impl.CanRefreshSchema;
			}
		}

		public ProxyControlDesignerImpl Impl
		{
			get
			{
				return this._impl;
			}
		}

		public override TemplateGroupCollection TemplateGroups
		{
			get
			{
				return this._impl.TemplateGroups;
			}
		}
	}

	public class ProxyDesignerDataSourceView : DesignerDataSourceView
	{
		// Fields
		private bool _isSPDataSource;
		private ProxyControlDesignerImpl _owner;
		private static readonly string FlatDataXPath = "/node()/node()";
		private static readonly string SPDataSourceFlatDataXPath = "*/Rows/Row";

		// Methods
		public ProxyDesignerDataSourceView(ProxyControlDesignerImpl owner)
			: base((IDataSourceDesigner)owner.Designer, string.Empty)
		{
			this._owner = owner;
			this._isSPDataSource = this._owner.Designer.Component.GetType().FullName.Equals("Microsoft.SharePoint.WebControls.SPDataSource");
		}

		public override IEnumerable GetDesignTimeData(int minimumRows, out bool isSampleData)
		{
			XmlDocument xmlDocument = this.XmlDocument;
			if (xmlDocument == null)
			{
				return base.GetDesignTimeData(minimumRows, out isSampleData);
			}
			isSampleData = false;
			return new ProxyDataEnumerable(xmlDocument.SelectNodes(this.XPath));
		}

		// Properties
		public override IDataSourceViewSchema Schema
		{
			get
			{
				XmlDocument xmlDocument = this.XmlDocument;
				if (xmlDocument != null)
				{
					IDataSourceSchema schema = new XmlDocumentSchema(xmlDocument, this.XPath);
					if (schema != null)
					{
						IDataSourceViewSchema[] views = schema.GetViews();
						if ((views != null) && (views.Length > 0))
						{
							return views[0];
						}
					}
				}
				return base.Schema;
			}
		}

		private XmlDocument XmlDocument
		{
			get
			{
				if (this._owner is ProxyHierarchicalDataSourceDesignerImpl)
				{
					return ((ProxyHierarchicalDataSourceDesignerImpl)this._owner).XmlDocument;
				}
				if (this._owner is ProxyDataSourceDesignerImpl)
				{
					return ((ProxyDataSourceDesignerImpl)this._owner).XmlDocument;
				}
				return null;
			}
		}

		private string XPath
		{
			get
			{
				if (this._isSPDataSource)
				{
					return SPDataSourceFlatDataXPath;
				}
				return FlatDataXPath;
			}
		}
	}

 


	public  class FpDataSourceProxyDesignerView : ProxyDesignerDataSourceView
	{
		// Methods
		public FpDataSourceProxyDesignerView(ProxyControlDesignerImpl owner)
			: base(owner)
		{
		}

		// Properties
		public override IDataSourceViewSchema Schema
		{
			get
			{
				IDataSourceViewSchema viewSchema = base.Schema;
				if (viewSchema != null)
				{
					return new FpDataSourceViewSchema(viewSchema);
				}
				return null;
			}
		}
	}
 
	public class FpHierarchicalDataSourceProxyDesigner : ProxyHierarchicalDataSourceControlDesigner, IDataSourceDesigner
	{
		// Events
		event EventHandler IDataSourceDesigner.DataSourceChanged
		{
			add
			{
				base.DataSourceChanged += value;
			}
			remove
			{
				base.DataSourceChanged -= value;
			}
		}

		event EventHandler IDataSourceDesigner.SchemaRefreshed
		{
			add
			{
				base.SchemaRefreshed += value;
			}
			remove
			{
				base.SchemaRefreshed -= value;
			}
		}

		// Methods
		public override void Configure()
		{
			ElementDesigner designer = base.Tag as ElementDesigner;
			if (designer != null)
			{
				IWebElement element = designer.Element;
				if (element != null)
				{
					element.LaunchFpDataSourceDesigner();
				}
			}
		}

		public override void Initialize(IComponent component)
		{
			base.Initialize(component);
		}

		void IDataSourceDesigner.Configure()
		{
			this.Configure();
		}

		DesignerDataSourceView IDataSourceDesigner.GetView(string viewName)
		{
			if ((base.Impl != null) && (base.Component.GetType().Assembly.GetName().Name.Equals("Microsoft.SharePoint") && base.Component.GetType().FullName.Equals("Microsoft.SharePoint.WebControls.SPXmlDataSource")))
			{
				return new FpDataSourceProxyDesignerView(base.Impl);
			}
			ProxyHierarchicalDataSourceDesignerImpl impl = base.Impl as ProxyHierarchicalDataSourceDesignerImpl;
			if (impl == null)
			{
				return null;
			}
			return impl.GetFlatView(viewName);
		}

		string[] IDataSourceDesigner.GetViewNames()
		{
			ProxyHierarchicalDataSourceDesignerImpl impl = base.Impl as ProxyHierarchicalDataSourceDesignerImpl;
			if (impl == null)
			{
				return null;
			}
			return impl.GetViewNames();
		}

		void IDataSourceDesigner.RefreshSchema(bool preferSilent)
		{
			this.RefreshSchema(preferSilent);
		}

		void IDataSourceDesigner.ResumeDataSourceEvents()
		{
			this.ResumeDataSourceEvents();
		}

		void IDataSourceDesigner.SuppressDataSourceEvents()
		{
			this.SuppressDataSourceEvents();
		}

		// Properties
		public override bool CanConfigure
		{
			get
			{
				return (base.Tag is ElementDesigner);
			}
		}

		bool IDataSourceDesigner.CanConfigure
		{
			get
			{
				return this.CanConfigure;
			}
		}

		bool IDataSourceDesigner.CanRefreshSchema
		{
			get
			{
				return this.CanRefreshSchema;
			}
		}
	}


}